#!/usr/bin/env python

import random
import time

import sys
try:
 	import pygtk
  	pygtk.require("2.0")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

class BrainAge:
	def getFakeAdd(self,a,b):
		offset=random.randint(1,3)
		neg=random.randint(0,1)
		if neg==0:
			return a+b+offset
		else:
			return a+b-offset

	def getFakeSub(self,a,b):
		offset=random.randint(1,3)
		neg=random.randint(0,1)
		if neg==0:
			return a-b+offset
		else:
			return a-b-offset

	def getFakeMult(self,a,b):
		while True:
			offset=random.randint(1,2)
			neg=random.randint(0,1)
			which=random.randint(0,1)
			if which==0:
				if neg==0:
					answ = (a+offset)*b
				else:
					answ = (a-offset)*b
			else:
				if neg==0:
					answ = a*(b+offset)
				else:
					answ = a*(b-offset)
		
			if answ != (a*b):
				return answ

	def __init__(self):
		#Set the Glade file
		self.started=False

		self.gladefile = "brainage.glade"  
	        self.wTree = gtk.glade.XML(self.gladefile) 

		self.scrolledproblems=self.wTree.get_widget("scrolledproblems")
		self.statuslabel=self.wTree.get_widget("statuslabel")
		
		#Create our dictionay and connect it
		dic = { "on_start_clicked" : self.startGame,
			"on_BrainAge_key_press_event" : self.keyPressed,
			"on_MainWindow_destroy" : gtk.main_quit }
		self.wTree.signal_autoconnect(dic)

	def keyPressed(self, widget, event):
		if self.started != True:
			return

		if self.problem_num > self.num_questions:
			return

		inp=gtk.gdk.keyval_name(event.keyval)

		if (self.fake==1 and inp=='n') or (self.fake==0 and inp=='y'):
			self.right_questions += 1
			templabel=gtk.Label("O")
			self.problembox.pack_end(templabel)
			templabel.show()
			self.problembox.show()
			self.problemsvbox.show()
		else:
			templabel=gtk.Label("X")
			self.problembox.pack_end(templabel)
			templabel.show()
			self.problembox.show()
			self.problemsvbox.show()

		if self.problem_num==self.num_questions:
			self.finish_time = time.time()
			self.statuslabel.set_text("You got "+str(100*self.right_questions/self.num_questions)+"% right in "+str(self.finish_time-self.init_time)+" seconds.")
			self.statuslabel.show()
			self.started=False
		else:
			self.nextProblem()

	def nextProblem(self):
		self.statuslabel.set_text("Problem #" + str(self.problem_num+1) + " of " + str(self.num_questions) + ".")

		a = random.randint(0,10)
		b = random.randint(0,10)
		choice = random.randint(0,2)
		self.fake=random.randint(0,1)

		if choice == 0:
			if self.fake==0:
				answ = a+b
			else:
				answ=self.getFakeAdd(a,b)
			operation = "+"
		elif choice == 1:
			if self.fake==0:
				answ=a-b
			else:
				answ=self.getFakeSub(a,b)
			operation = "-"
		else  :
			if self.fake==0:
				answ=a*b
			else:
				answ=self.getFakeMult(a,b)
			operation = "x"

		templabel=gtk.Label(str(a)+" "+operation+" "+str(b)+" = "+str(answ)+" ")
		templabel.set_size_request(400,100)
		self.problembox=gtk.HBox()
		self.problembox.pack_start(templabel)

		self.problemsvbox.pack_end(self.problembox)
		templabel.show()
		self.problembox.show()
		self.problemsvbox.show()
		
		self.problem_num+=1


	def startGame(self,widget):
		self.init_time = time.time()
		self.num_questions = 15
		self.problem_num = 0
		self.right_questions = 0
		self.started=True

		self.problemsvbox=gtk.VBox()
		self.scrolledproblems.add_with_viewport(self.problemsvbox)
		self.problemsvbox.show()

		self.nextProblem()

	#def btnHelloWorld_clicked(self, widget):
	#	print "Hello World!"


if __name__ == "__main__":
	hwg = BrainAge()
	gtk.main()
